package cn.netuo.bumbersoll.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.rooten.base.DBBase;

import java.util.ArrayList;

import cn.netuo.bumbersoll.entity.Cmd;
import cn.netuo.bumbersoll.util.BsUtil;
import lib.grasp.util.L;

/**
 * 业务命令表
 */
public class TableCmd extends DBBase {

    private final String    TB_NAME   	            = "tb_cmd";

    private final String    COL_REQUEST_NAME            = "requestName";        // 命令别名(英文)
    private final String    COL_SERVICE_ID              = "serviceId";          // 服务ID
    private final String    COL_CHART_ID                = "chartId";            // 特征ID
    private final String    COL_TARGET_VALUE            = "targetValue";        // 具体指令
    private final String    COL_REMARK                  = "remark";             // 备注

    public TableCmd() {
        createTable();
    }

    //建表
    private boolean createTable() {
        if (tableExists(TB_NAME) && !hasColumn(TB_NAME, COL_REQUEST_NAME)) {    // 存在表结构不对的同名表
            dropTable(TB_NAME);
            return createTable();
        }
        else if (tableExists(TB_NAME)) {    // 存在表
            return true;
        }
        try {
            String strFmt = "CREATE TABLE %s ("
                    + "%s TEXT PRIMARY KEY,"        //主键
                    + "%s TEXT ,"
                    + "%s TEXT ,"
                    + "%s TEXT ,"
                    + "%s TEXT "
                    + ");";

            String strSql = String.format(strFmt, 	TB_NAME,
                    COL_REQUEST_NAME,
                    COL_SERVICE_ID  ,
                    COL_CHART_ID    ,
                    COL_TARGET_VALUE,
                    COL_REMARK
            );
            L.logOnly("建表--SQL:" + strSql);
            execSQL(strSql);
            return true;
        }
        catch (SQLException e) {
            L.logOnly("SQLException" + e.getMessage());
        }
        return false;
    }

    //删表
    public void dropTable() {
        if (tableExists(TB_NAME)) dropTable(TB_NAME);
    }

    //增/改
    public void importArg(Cmd cmd) {
        if(cmd == null) return;
        ContentValues cv = new ContentValues();
        cv.put(COL_REQUEST_NAME 		, cmd.requestName);
        cv.put(COL_SERVICE_ID   		, cmd.serviceId);
        cv.put(COL_CHART_ID     		, cmd.chartId);
        cv.put(COL_TARGET_VALUE 		, BsUtil.stringList2String(cmd.targetValue));
        cv.put(COL_REMARK       		, cmd.remark);

//        insert(TB_NAME, cv);
        replace(TB_NAME, cv);
    }

    //查, 返回的是个实体
    @Nullable
    public Cmd queryCmdByArgName(String argName) {
        if(TextUtils.isEmpty(argName)) return null;
        ArrayList<String> alarm_List = new ArrayList<>();
        String sql = "select %s from %s where %s = '%s';";
        sql = String.format(sql, "*", TB_NAME, COL_REQUEST_NAME, argName);
        L.logOnly("查--SQL:"+sql);

        Cmd cmd = new Cmd();
        Cursor result = rawQuery(sql);
        try {
            for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()){  // 使用游标获取结果{
                String request_name   = result.getString(result.getColumnIndex(COL_REQUEST_NAME));
                String service_id     = result.getString(result.getColumnIndex(COL_SERVICE_ID  ));
                String chart_id       = result.getString(result.getColumnIndex(COL_CHART_ID    ));
                String target_value   = result.getString(result.getColumnIndex(COL_TARGET_VALUE));
                String remark         = result.getString(result.getColumnIndex(COL_REMARK      ));

                cmd.requestName = request_name;
                cmd.serviceId = service_id  ;
                cmd.chartId = chart_id    ;

                cmd.targetValue = new ArrayList<>();
                cmd.targetValue.addAll(BsUtil.string2StringList(target_value));

                cmd.remark = remark      ;
            }
        }
        catch (Exception e){
            L.logOnly("Exception--" + e);
        }
        finally {
            closeAll(result);
        }
        return cmd;
    }
}
